package com.coocaa.app.gamecenter.pages.list;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.coocaa.launcher.framework.sidebar.ItemContainer.IItem;
import com.coocaa.launcher.framework.sidebar.item.ItemFactory;
import com.coocaa.launcher.framework.sidebar.item.ItemFactory.ITEM;
import com.tianci.appstore.ui.data.SkyListAppData;

import java.util.ArrayList;
import java.util.List;

class GameListAdapter extends BaseAdapter {
    private Context mContext = null;
    private List<SkyListAppData> mListAppDatas = new ArrayList<SkyListAppData>();
    private List<IItem> mItems = new ArrayList<IItem>();

    public GameListAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mListAppDatas == null ? 0 : mListAppDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mListAppDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        IItem item = null;
        if (convertView == null) {
            item = ItemFactory.createItem(ITEM.GAME_LIST, mContext);
            mItems.add(item);
            convertView = item.getView();
            convertView.setTag(item);
        } else {
            item = (IItem) convertView.getTag();
        }
        item.setData(mListAppDatas.get(position));
        item.setIndex(position);
        return convertView;
    }

    public void updateUI(List<SkyListAppData> appDatas) {
        this.mListAppDatas.addAll(appDatas);
        notifyDataSetChanged();
    }
    public void destory() {
        for (IItem item : mItems) {
            item.destory();
        }
    }
}